package com.sanpu.kxe.module.dialog;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseDialog;
import com.sanpu.kxe.module.roundview.RoundTextView;

import butterknife.ButterKnife;

/**
 * Description：带中间顶部大图（有突出部分）的dialog
 * Created by 薛勇军 on 2018/7/9.
 */
public class GlobalDialog extends BaseDialog<GlobalDialog> {
    private RoundTextView close;

    private CharSequence content;
    private TextView vContent;
    private TextView vTitle;
    private int headerIcon;
    private String title;
    private int gravity = 0;
    private boolean hasCloseBtn = true;

    public GlobalDialog content(CharSequence content) {
        this.content = content;
        return this;
    }

    public void setContent(CharSequence content) {
        this.content = content;
    }

    public GlobalDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        showAnim(new BaseAnimatorSet() {
            @Override
            public void setAnimation(View view) {

            }
        });
        View inflate = View.inflate(getContext(), R.layout.dialog_global, null);
        vContent = ButterKnife.findById(inflate, R.id.content);
        vTitle = ButterKnife.findById(inflate, R.id.title);
        close = ButterKnife.findById(inflate, R.id.close);
        inflate.setPadding(dp2px(15), dp2px(15), dp2px(15), dp2px(15));

        if (headerIcon != 0) {
            ButterKnife.findById(inflate, R.id.header).setBackgroundResource(headerIcon);
        }
        if (!TextUtils.isEmpty(title)) {
            vTitle.setVisibility(View.VISIBLE);
            vTitle.setText(title);
        } else {
            vTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(content)) {
            vContent.setVisibility(View.VISIBLE);
            vContent.setText(content);
        } else {
            vContent.setVisibility(View.GONE);
        }
        if (gravity > 0) {
            vContent.setGravity(gravity);
        }
        close.setVisibility(hasCloseBtn ? View.VISIBLE : View.GONE);
        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public GlobalDialog headerIcon(@DrawableRes int headerIcon) {
        this.headerIcon = headerIcon;
        return this;
    }

    public GlobalDialog title(String title) {
        this.title = title;
        return this;
    }

    public GlobalDialog gravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public GlobalDialog hasCloseBtn(boolean hasCloseBtn) {
        this.hasCloseBtn = hasCloseBtn;
        return this;
    }
}